﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data.Common;

namespace SqlPaging
{
    /// <summary>
    /// Adds server side paging capability to the SqlDataSource.
    /// </summary>    
    public class SqlPagingDataSource : SqlDataSource
    {
        private SqlPagingDataSourceView _view;       
        
        [DefaultValue(false), Category("Paging")]
        public bool EnablePaging
        {
            get
            {
                return this.GetView().EnablePaging;
            }
            set
            {
                this.GetView().EnablePaging = value;
            }
        }

        private SqlPagingDataSourceView GetView()
        {
            if(_view == null)
                _view = (SqlPagingDataSourceView)this.GetView("DefaultView");
            
            return _view;            
        }

        /// <summary>
        /// Overrides the base class method, to use our custom SqlPagingDataSourceView instead
        /// of the built-in SqlDataSourceView, so that paging can be handled by the database server.
        /// </summary>
        /// <param name="viewName">The name of the data source view.
        /// <value>The SqlDataSourceView restricts the value of this parameter to "DefaultView".</value>
        /// </param>
        /// <returns></returns>
        protected override SqlDataSourceView CreateDataSourceView(string viewName)
        {
            return (SqlDataSourceView)new SqlPagingDataSourceView(this, viewName, this.Context);
        }

        [Category("Paging")]
        public string MaximumRowsParameterName
        {
            get
            {
                return this.GetView().MaximumRowsParameterName;
            }
            set
            {
                this.GetView().MaximumRowsParameterName = value;
            }
        }

        [Category("Paging")]
        public string StartRowIndexParameterName
        {
            get
            {
                return this.GetView().StartRowIndexParameterName;
            }
            set
            {
                this.GetView().StartRowIndexParameterName = value;
            }
        }

        /// <summary>
        /// Currently the parameter is passed to the database server to indicate that
        /// only the count should be returned instead of the actual results.
        /// In the future we will implement other options such as using an output parameter
        /// or having a separate select count command.        
        /// </summary>
        [Category("Paging")]
        public string CountParameterName
        {
            get
            {
                return this.GetView().CountParameterName;
            }
            set
            {
                this.GetView().CountParameterName = value;
            }
        }

        internal DbParameter CreateParameter(string Name, object value)
        {
            DbParameter prm = this.GetDbProviderFactory().CreateParameter();
            prm.ParameterName = Name;
            prm.Value = value;
            return prm;
        }
    }
}
